package com.linkage.educloud.ah.mvp.presenter;

import android.content.Context;

import com.linkage.educloud.ah.bean.AppClassify;
import com.linkage.educloud.ah.bean.HomeResBean;
import com.linkage.educloud.ah.bean.ResClassifyMeta;
import com.linkage.educloud.ah.bean.StudyTypeBean;
import com.linkage.educloud.ah.http.Method;
import com.linkage.educloud.ah.http.RequestUtil;
import com.linkage.educloud.ah.mvp.contract.IAppContract;
import com.linkage.educloud.ah.mvp.model.AppModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.BaseSubscriber;
import com.zzhou.common.library.util.ToastShow;

import org.json.JSONException;
import org.json.JSONObject;


import io.reactivex.Observable;

/**
 * Created by Mr'Tang on 2018/12/17.
 */

public class AppPresenter extends IAppContract.IPresenter<IAppContract.IView> {
    Context mContext;
    IAppContract.IModel iModel;

    public AppPresenter(Context context, IAppContract.IView iView) {
        super(iView);
        mContext = context;
        iModel = new AppModel();
    }

    @Override
    public void getAppClassify() {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.getAppClassify());
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 0) {
                                ((IAppContract.IView) iView).initAppClassify(AppClassify.parseFromJson(json.optJSONArray("data")));
                                ((IAppContract.IView) iView).initRadioButton(ResClassifyMeta.fromJSONObject(json.optJSONArray("commanddata")));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }


    @Override
    public void getResClassify() {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.getResClassify());
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 0) {
                                ((IAppContract.IView) iView).initSubjectTab(StudyTypeBean.fromJSONObject(json));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }

    @Override
    public void getRecommends(int subjectid, int gradeid, int page) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.getRecommends(subjectid,gradeid,0,10,page));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 0) {
                                ((IAppContract.IView) iView).initReslist(json.optString("title"), HomeResBean.parseFromJson(json.optJSONArray("data")));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }
}